11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
14 define( 'MSG_CACHE_VERSION', 1 );
18 * Performs various MediaWiki namespace-related functions
23 var $mCache, $mUseCache, $mDisable, $mExpiry;
24 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
25 var $mExtensionMessages = array();
26 var $mInitialised = false;
27 var $mDeferred = true;
29 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
30 wfProfileIn( __METHOD__
);
32 $this->mUseCache
= !is_null( $memCached );
33 $this->mMemc
= &$memCached;
34 $this->mDisable
= !$useDB;
35 $this->mExpiry
= $expiry;
36 $this->mDisableTransform
= false;
37 $this->mMemcKey
= $memcPrefix.':messages';
38 $this->mKeys
= false; # initialised on demand
39 $this->mInitialised
= true;
40 $this->mParser
= null;
42 # When we first get asked for a message,
43 # then we'll fill up the cache. If we
44 # can return a cache hit, this saves
45 # some extra milliseconds
46 $this->mDeferred
= true;
48 wfProfileOut( __METHOD__
);
51 function getParserOptions() {
52 if ( !$this->mParserOptions
) {
53 $this->mParserOptions
= new ParserOptions
;
55 return $this->mParserOptions
;
59 * Try to load the cache from a local file
61 function loadFromLocal( $hash ) {
62 global $wgLocalMessageCache;
64 $this->mCache
= false;
65 if ( $wgLocalMessageCache === false ) {
69 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
72 $file = fopen( $filename, 'r' );
78 // Check to see if the file has the hash specified
79 $localHash = fread( $file, 32 );
80 if ( $hash == $localHash ) {
81 // All good, get the rest of it
82 $serialized = fread( $file, 10000000 );
83 $this->setCache( unserialize( $serialized ) );
89 * Save the cache to a local file
91 function saveToLocal( $serialized, $hash ) {
92 global $wgLocalMessageCache;
94 if ( $wgLocalMessageCache === false ) {
98 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
99 $oldUmask = umask( 0 );
100 wfMkdirParents( $wgLocalMessageCache, 0777 );
103 $file = fopen( $filename, 'w' );
105 wfDebug( "Unable to open local cache file for writing\n" );
109 fwrite( $file, $hash . $serialized );
111 @chmod
( $filename, 0666 );
114 function loadFromScript( $hash ) {
115 global $wgLocalMessageCache;
116 if ( $wgLocalMessageCache === false ) {
120 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
122 wfSuppressWarnings();
123 $file = fopen( $filename, 'r' );
128 $localHash=substr(fread($file,40),8);
130 if ($hash!=$localHash) {
133 require("$wgLocalMessageCache/messages-" . wfWikiID());
134 $this->setCache( $this->mCache
);
137 function saveToScript($array, $hash) {
138 global $wgLocalMessageCache;
139 if ( $wgLocalMessageCache === false ) {
143 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
144 $oldUmask = umask( 0 );
145 wfMkdirParents( $wgLocalMessageCache, 0777 );
147 $file = fopen( $filename.'.tmp', 'w');
148 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
150 foreach ($array as $key => $message) {
151 fwrite($file, "'". $this->escapeForScript($key).
152 "' => '" . $this->escapeForScript($message).
155 fwrite($file,");\n?>");
157 rename($filename.'.tmp',$filename);
160 function escapeForScript($string) {
161 $string = str_replace( '\\', '\\\\', $string );
162 $string = str_replace( '\'', '\\\'', $string );
167 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
169 function setCache( $cache ) {
170 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION
) {
171 $this->mCache
= $cache;
173 $this->mCache
= false;
178 * Loads messages either from memcached or the database, if not disabled
179 * On error, quietly switches to a fallback mode
180 * Returns false for a reportable error, true otherwise
183 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
185 if ( $this->mDisable
) {
186 static $shownDisabled = false;
187 if ( !$shownDisabled ) {
188 wfDebug( "MessageCache::load(): disabled\n" );
189 $shownDisabled = true;
193 if ( !$this->mUseCache
) {
194 $this->mDeferred
= false;
198 $fname = 'MessageCache::load';
199 wfProfileIn( $fname );
202 $this->mCache
= false;
205 wfProfileIn( $fname.'-fromlocal' );
206 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
208 if ($wgLocalMessageCacheSerialized) {
209 $this->loadFromLocal( $hash );
211 $this->loadFromScript( $hash );
213 if ( $this->mCache
) {
214 wfDebug( "MessageCache::load(): got from local cache\n" );
217 wfProfileOut( $fname.'-fromlocal' );
220 if ( !$this->mCache
) {
221 wfProfileIn( $fname.'-fromcache' );
222 $this->setCache( $this->mMemc
->get( $this->mMemcKey
) );
223 if ( $this->mCache
) {
224 wfDebug( "MessageCache::load(): got from global cache\n" );
225 # Save to local cache
226 if ( $wgLocalMessageCache !== false ) {
227 $serialized = serialize( $this->mCache
);
229 $hash = md5( $serialized );
230 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
232 if ($wgLocalMessageCacheSerialized) {
233 $this->saveToLocal( $serialized,$hash );
235 $this->saveToScript( $this->mCache
, $hash );
239 wfProfileOut( $fname.'-fromcache' );
243 # If there's nothing in memcached, load all the messages from the database
244 if ( !$this->mCache
) {
245 wfDebug( "MessageCache::load(): cache is empty\n" );
247 # Other threads don't need to load the messages if another thread is doing it.
248 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
250 wfProfileIn( $fname.'-load' );
251 wfDebug( "MessageCache::load(): loading all messages from DB\n" );
253 wfProfileOut( $fname.'-load' );
256 # Keep trying if it fails, this is kind of important
257 wfProfileIn( $fname.'-save' );
259 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
261 usleep(mt_rand(500000,1500000));
264 # Save to local cache
265 if ( $wgLocalMessageCache !== false ) {
266 $serialized = serialize( $this->mCache
);
267 $hash = md5( $serialized );
268 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
269 if ($wgLocalMessageCacheSerialized) {
270 $this->saveToLocal( $serialized,$hash );
272 $this->saveToScript( $this->mCache
, $hash );
276 wfProfileOut( $fname.'-save' );
278 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
279 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
281 $this->mMemc
->delete( $this->mMemcKey
.'-status' );
287 if ( !is_array( $this->mCache
) ) {
288 wfDebug( "MessageCache::load(): unable to load cache, disabled\n" );
289 $this->mDisable
= true;
290 $this->mCache
= false;
292 wfProfileOut( $fname );
293 $this->mDeferred
= false;
298 * Loads all or main part of cacheable messages from the database
300 function loadFromDB() {
301 global $wgLang, $wgMaxMsgCacheEntrySize;
303 wfProfileIn( __METHOD__
);
304 $dbr =& wfGetDB( DB_SLAVE
);
305 $this->mCache
= array();
307 # Load titles for all oversized pages in the MediaWiki namespace
308 $res = $dbr->select( 'page', 'page_title',
310 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
311 'page_is_redirect' => 0,
312 'page_namespace' => NS_MEDIAWIKI
,
315 while ( $row = $dbr->fetchObject( $res ) ) {
316 $this->mCache
[$row->page_title
] = '!TOO BIG';
318 $dbr->freeResult( $res );
320 # Load text for the remaining pages
321 $res = $dbr->select( array( 'page', 'revision', 'text' ),
322 array( 'page_title', 'old_text', 'old_flags' ),
324 'page_is_redirect' => 0,
325 'page_namespace' => NS_MEDIAWIKI
,
326 'page_latest=rev_id',
327 'rev_text_id=old_id',
328 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
331 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
332 $this->mCache
[$row->page_title
] = ' ' . Revision
::getRevisionText( $row );
334 $this->mCache
['VERSION'] = MSG_CACHE_VERSION
;
335 $dbr->freeResult( $res );
336 wfProfileOut( __METHOD__
);
340 * Not really needed anymore
344 if ( !$this->mKeys
) {
345 $this->mKeys
= array();
346 $allMessages = Language
::getMessagesFor( 'en' );
347 foreach ( $allMessages as $key => $unused ) {
348 $title = $wgContLang->ucfirst( $key );
349 array_push( $this->mKeys
, $title );
355 function replace( $title, $text ) {
356 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
357 global $wgMaxMsgCacheEntrySize;
359 wfProfileIn( __METHOD__
);
362 $parserMemc->delete(wfMemcKey('sidebar'));
363 if ( is_array( $this->mCache
) ) {
364 if ( $text === false ) {
365 # Article was deleted
366 unset( $this->mCache
[$title] );
367 $this->mMemc
->delete( "$this->mMemcKey:{$title}" );
368 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
369 $this->mCache
[$title] = '!TOO BIG';
370 $this->mMemc
->set( "$this->mMemcKey:{$title}", ' '.$text, $this->mExpiry
);
372 $this->mCache
[$title] = ' ' . $text;
373 $this->mMemc
->delete( "$this->mMemcKey:{$title}" );
375 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
377 # Save to local cache
378 if ( $wgLocalMessageCache !== false ) {
379 $serialized = serialize( $this->mCache
);
380 $hash = md5( $serialized );
381 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
382 if ($wgLocalMessageCacheSerialized) {
383 $this->saveToLocal( $serialized,$hash );
385 $this->saveToScript( $this->mCache
, $hash );
390 wfProfileOut( __METHOD__
);
395 * Represents a write lock on the messages key
398 if ( !$this->mUseCache
) {
402 $lockKey = $this->mMemcKey
. 'lock';
403 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
407 return $i >= MSG_WAIT_TIMEOUT
;
411 if ( !$this->mUseCache
) {
415 $lockKey = $this->mMemcKey
. 'lock';
416 $this->mMemc
->delete( $lockKey );
420 * Get a message from either the content language or the user language.
422 * @param string $key The message cache key
423 * @param bool $useDB Get the message from the DB, false to use only the localisation
424 * @param bool $forContent Get the message from the content language rather than the
426 * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
428 function get( $key, $useDB = true, $forContent = true, $isFullKey = false ) {
429 global $wgContLanguageCode, $wgContLang, $wgLang;
431 $lang =& $wgContLang;
435 $langcode = $lang->getCode();
436 # If uninitialised, someone is trying to call this halfway through Setup.php
437 if( !$this->mInitialised
) {
438 return '<' . htmlspecialchars($key) . '>';
440 # If cache initialization was deferred, start it now.
441 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
447 # Normalise title-case input
448 $lckey = $wgContLang->lcfirst( $key );
450 # Try the MediaWiki namespace
451 if( !$this->mDisable
&& $useDB ) {
452 $title = $wgContLang->ucfirst( $key );
453 if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
454 $title .= '/' . $langcode;
456 $message = $this->getMsgFromNamespace( $title );
458 # Try the extension array
459 if( $message === false && isset( $this->mExtensionMessages
[$langcode][$lckey] ) ) {
460 $message = $this->mExtensionMessages
[$langcode][$lckey];
462 if ( $message === false && isset( $this->mExtensionMessages
['en'][$lckey] ) ) {
463 $message = $this->mExtensionMessages
['en'][$lckey];
466 # Try the array in the language object
467 if( $message === false ) {
468 #wfDebug( "Trying language object for message $key\n" );
469 wfSuppressWarnings();
470 $message = $lang->getMessage( $lckey );
472 if ( is_null( $message ) ) {
477 # Try the array of another language
478 if( $message === false && strpos( $lckey, '/' ) ) {
479 $message = explode( '/', $lckey );
481 wfSuppressWarnings();
482 $message = Language
::getMessageFor( $message[0], $message[1] );
484 if ( is_null( $message ) ) {
492 # Is this a custom message? Try the default language in the db...
493 if( ($message === false ||
$message === '-' ) &&
494 !$this->mDisable
&& $useDB &&
495 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
496 $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $key ) );
500 if( $message === false ) {
501 return '<' . htmlspecialchars($key) . '>';
505 $message = $this->transform( $message );
510 * Get a message from the MediaWiki namespace, with caching. The key must
511 * first be converted to two-part lang/msg form if necessary.
513 * @param string $title Message cache key with initial uppercase letter
515 function getMsgFromNamespace( $title ) {
520 if( $this->mUseCache
&& isset( $this->mCache
[$title] ) ) {
521 $entry = $this->mCache
[$title];
522 $type = substr( $entry, 0, 1 );
523 if ( $type == ' ' ) {
524 return substr( $entry, 1 );
528 # Call message hooks, in case they are defined
529 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
530 if ( $message !== false ) {
534 # If there is no cache entry and no placeholder, it doesn't exist
535 if ( $type != '!' && $message === false ) {
539 $memcKey = $this->mMemcKey
. ':' . $title;
541 # Try the individual message cache
542 if ( $this->mUseCache
) {
543 $entry = $this->mMemc
->get( $memcKey );
545 $type = substr( $entry, 0, 1 );
547 if ( $type == ' ' ) {
548 $message = substr( $entry, 1 );
549 $this->mCache
[$title] = $message;
551 } elseif ( $entry == '!NONEXISTENT' ) {
554 # Corrupt/obsolete entry, delete it
555 $this->mMemc
->delete( $memcKey );
561 # Try loading it from the DB
562 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
564 $message = $revision->getText();
565 if ($this->mUseCache
) {
566 $this->mCache
[$title] = ' ' . $message;
567 $this->mMemc
->set( $memcKey, $message, $this->mExpiry
);
571 # Use some special text instead of false, because false gets converted to '' somewhere
572 $this->mMemc
->set( $memcKey, '!NONEXISTENT', $this->mExpiry
);
573 $this->mCache
[$title] = false;
579 function transform( $message ) {
581 if ( !$this->mParser
&& isset( $wgParser ) ) {
582 # Do some initialisation so that we don't have to do it twice
583 $wgParser->firstCallInit();
584 # Clone it and store it
585 $this->mParser
= clone $wgParser;
587 if ( !$this->mDisableTransform
&& $this->mParser
) {
588 if( strpos( $message, '{{' ) !== false ) {
589 $message = $this->mParser
->transformMsg( $message, $this->getParserOptions() );
595 function disable() { $this->mDisable
= true; }
596 function enable() { $this->mDisable
= false; }
597 function disableTransform() { $this->mDisableTransform
= true; }
598 function enableTransform() { $this->mDisableTransform
= false; }
599 function setTransform( $x ) { $this->mDisableTransform
= $x; }
600 function getTransform() { return $this->mDisableTransform
; }
603 * Add a message to the cache
606 * @param mixed $value
607 * @param string $lang The messages language, English by default
609 function addMessage( $key, $value, $lang = 'en' ) {
610 $this->mExtensionMessages
[$lang][$key] = $value;
614 * Add an associative array of message to the cache
616 * @param array $messages An associative array of key => values to be added
617 * @param string $lang The messages language, English by default
619 function addMessages( $messages, $lang = 'en' ) {
620 wfProfileIn( __METHOD__
);
621 if ( isset( $this->mExtensionMessages
[$lang] ) ) {
622 $this->mExtensionMessages
[$lang] = $messages +
$this->mExtensionMessages
[$lang];
624 $this->mExtensionMessages
[$lang] = $messages;
626 wfProfileOut( __METHOD__
);
630 * Add a 2-D array of messages by lang. Useful for extensions.
631 * Introduced in 1.9. Please do not use it for now, for backwards compatibility.
633 * @param array $messages The array to be added
635 function addMessagesByLang( $messages ) {
636 wfProfileIn( __METHOD__
);
637 foreach ( $messages as $key => $value ) {
638 $this->addMessages( $value, $key );
640 wfProfileOut( __METHOD__
);
644 * Get the extension messages for a specific language
646 * @param string $lang The messages language, English by default
648 function getExtensionMessagesFor( $lang = 'en' ) {
649 wfProfileIn( __METHOD__
);
651 if ( isset( $this->mExtensionMessages
[$lang] ) ) {
652 $messages = $this->mExtensionMessages
[$lang];
654 if ( $lang != 'en' ) {
655 $messages = $messages +
$this->mExtensionMessages
['en'];
657 wfProfileOut( __METHOD__
);
662 * Clear all stored messages. Mainly used after a mass rebuild.
665 global $wgLocalMessageCache;
666 if( $this->mUseCache
) {
668 $this->mMemc
->delete( $this->mMemcKey
);
669 # Invalidate all local caches
670 $this->mMemc
->delete( "{$this->mMemcKey}-hash" );
674 static function loadAllMessages() {
675 # Some extensions will load their messages when you load their class file
676 wfLoadAllExtensions();
677 # Others will respond to this hook
678 wfRunHooks( 'LoadAllMessages' );
679 # Still others will respond to neither, they are EVIL. We sometimes need to know!